package com.small.main;

import com.small.service.DealMethod;
import com.small.utils.LogUtil;
import com.small.utils.PropertiesUtil;

import javax.lang.model.SourceVersion;
import java.io.File;
import java.io.IOException;
import java.sql.SQLOutput;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author syd
 * @create 2020-12-12 8:34
 */
public class DealMain {

    public static Logger log = Logger.getGlobal();

    static {
        Properties prop = new Properties();
        PropertiesUtil propertiesUtil = new PropertiesUtil();
        try {
            propertiesUtil.getProperties(prop);
        } catch (IOException e) {
            e.printStackTrace();
        }
//由于jdk自带的全局log没有写入文件的功能，我这里手动添加了文件handler
        LogUtil.addFileHandler(log, Level.INFO,  prop.getProperty("deal.log")+ File.separator + "deal.log");
    }

    public static void main(String[] args) throws Exception {
        log.info("开始处理文件！");

        //0.获取文件列表
        DealMethod dealMethod = new DealMethod();
        String in=dealMethod.getPath("in");

        File file = new File(in);

        //1.校验输入目录是否为空
        if(file.list().length==0){
            log.warning("目录为空，请添加文件！");
            return;
        }

        //2.处理文件
        File[] files = file.listFiles();

        for (File fe : files) {

            String suffix = fe.getName().substring(fe.getName().lastIndexOf(".") + 1);

            if (fe.isDirectory()||suffix.equals("csv")) {
                continue;
            }
            List<Map<String, String>> maps = dealMethod.dealFile(fe);

            //输出到csv文件
            String name=fe.getName();
            dealMethod.exportCsv(maps,name);
        }

        //3.输出为csv文件
        log.info("结束处理文件！");
    }

}
